﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;

namespace PMCSerialTransfer
{

    public partial class frmConfig : Form
    {
        private SerialSettings mConfig;
        private string rsltMessage;
        private Propellent _propNet;

        public SerialSettings Configure
        {
            get { return mConfig; }
            set { mConfig = value; }
        }

        public string Message
        {
            get { return rsltMessage; }
        }

        public frmConfig()
        {
            InitializeComponent();

        }

        public void InitializeValues(SerialSettings settings)
        {
            
            string popComm;
            char[] delimiterChars = { ',' };

            _propNet = new Propellent(this.Handle, false, null);
            mConfig = settings;
            mConfig.GetSettings();
            //commPorts = SerialPort.GetPortNames();
            popComm = _propNet.GetComPorts(true);
            mConfig.PortNameCollection = popComm.Split(delimiterChars);
            lstComm.DataSource = mConfig.PortNameCollection;
            lstComm.SelectedItem = mConfig.PortName;
            lstBaud.DataSource = mConfig.BaudRateCollection;
            lstBaud.SelectedItem = mConfig.BaudRate;
            txtFolder.Text = Configure.DefaultFolder;
            txtBinary.Text = Configure.BinaryFile;
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Show the FolderBrowserDialog.
            if (Configure.DefaultFolder.Length>0)
                folderBrowserDialog1.SelectedPath = Configure.DefaultFolder;
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtFolder.Text = folderBrowserDialog1.SelectedPath;
                Configure.DefaultFolder = folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            rsltMessage = "Configuration Canceled\n";
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Configure.BaudRate = Convert.ToInt32(lstBaud.SelectedItem);
            Configure.PortName = (string)lstComm.SelectedItem;
            Configure.DefaultFolder = txtFolder.Text;
            Configure.SaveSettings();
            rsltMessage = "Comm Port: " + Configure.PortName + "\nBaud Rate: " + Convert.ToString(Configure.BaudRate) + "\nDefault Folder: " + Configure.DefaultFolder + "\nBinary File: "+ Configure.BinaryFile + "\n";
            this.Close();
        }

        private void frmConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (rsltMessage == null)
                rsltMessage = "Configuration Canceled\n";
            _propNet.Dispose();
        }

        private void btnScan_Click(object sender, EventArgs e)
        {
            string popComm;
            char[] delimiterChars = { ',' };
            _propNet.ShowEditPortDialog();
            popComm = _propNet.GetComPorts(true);
            mConfig.PortNameCollection = popComm.Split(delimiterChars);
            lstComm.DataSource = mConfig.PortNameCollection;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            

            // Show the FolderBrowserDialog.
            if (Configure.BinaryFile.Length > 0)
                openFileDialog1.InitialDirectory = Path.GetDirectoryName(Configure.BinaryFile);
            openFileDialog1.Filter = "Binary files (*.binary)|*.binary|All files (*.*)|*.*";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtBinary.Text = openFileDialog1.FileName;
                Configure.BinaryFile = openFileDialog1.FileName;
            }
        }
    }
}
